﻿using Forum.Model.Entity.System;
using System.Security.Claims;

namespace Forum.BaseController
{
    [ApiController]
    //[Authorize]
    [ApiExplorerSettings(GroupName = nameof(ApiVersions.BasicInfo))]
    public class BaseController<TDefaultService> : ControllerBase where TDefaultService : class
    {
        protected readonly TDefaultService DefaultService;
        /// <summary>
        /// 日志
        /// </summary>
        protected readonly ILogger<BaseController<TDefaultService>> Log;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="defaultService">默认服务</param>
        /// <param name="logger">默认服务</param>
        public BaseController(TDefaultService defaultService, ILogger<BaseController<TDefaultService>> logger)
        {
            DefaultService = defaultService;
            Log = logger;
        }

        ///// <summary>
        ///// 用户Id
        ///// </summary>
        //protected virtual string? UserId => CurrentUser.UserCode;
        ///// <summary>
        ///// 用户信息
        ///// </summary>
        public SYS_USER? CurrentUser
        {
            get
            {
                try
                {
                    var user = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                    if (user != null)
                        return JsonConvert.DeserializeObject<SYS_USER>(user);
                    else
                        return null;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
    }
}
